﻿using UnityEngine;
using System.Collections;

public class VisualSideBoardManager : MonoBehaviour {
    
    public ActionProcessor actionProcessor;

    public float spaceBetween;

    public GameObject gridSquare;

    public GameObject[,] myBoard;
    GameObject[,] myToyBox;


    public GameObject[] buildingList;
	// Use this for initialization
	void Start () {
        myBoard = new GameObject[actionProcessor.boardWidth, actionProcessor.boardHeight];
        myToyBox = new GameObject[actionProcessor.boardWidth, actionProcessor.boardHeight];
        Vector3 space = new Vector3();
        
        for (int i = 0; i < actionProcessor.boardWidth; i++)
        {
            
            for (int j = 0; j < actionProcessor.boardHeight; j++)
            {
                space.z = j*-(1+spaceBetween);
                space.x = i*-(1+spaceBetween);
                myBoard[i,j]= (GameObject) Instantiate(gridSquare, space, new Quaternion());
                myBoard[i, j].transform.parent = this.transform;
                myBoard[i, j].name = "[" + i + ", " + j + "]";
                
            }

        }
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    public void removePiece(int x,int y)
    {
        print(string.Format("Removing piece at ({0}, {1})", x, y));
        Network.Destroy(myToyBox[x, y]);
    }

    public void addPiece(int x, int y, int name)
    {
        if (name == 0)
        {
            return;
        }

        GameObject subject = null;
        subject = GlobalVariables.buildingObjectList[name];
        
        Vector3 space = new Vector3();
        space.z = y * -(1 + spaceBetween);
        space.x = x * -(1 + spaceBetween);
        space.y = .45f;

        print(string.Format("Adding piece {0} at ({1}, {2}), world position {3}", name, x, y, space));

        if (subject != null)
        {
            myToyBox[x, y] = Network.Instantiate(subject, space, Quaternion.identity, 0) as GameObject;
            //myToyBox[x, y] = (GameObject)Instantiate(subject, space, new Quaternion());
            myToyBox[x, y].transform.parent = myBoard[x, y].transform;
        }

    }
}
